# Copyright (c) 2022-2023, Arm Limited and Contributors. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

# TF-M example

set(example tf-m)
set(supported_platforms corstone-300 corstone-310)
set(supported_rtoses cmsis-rtx cmsis-threadx)

foreach(platform IN LISTS supported_platforms)
    get_tfm_target_platform_variables(${platform})

    foreach(rtos IN LISTS supported_rtoses)
        get_target_rtos_variables(${rtos})
        set(EXAMPLE_TITLE "Trusted Firmware-M (TF-M) example")
        set(EXAMPLE_BINARY_CRYPTO "__build/iotsdk-example-tf-m-crypto_merged.elf")
        set(EXAMPLE_BINARY_FIRMWARE_UPDATE "__build/iotsdk-example-tf-m-firmware-update_merged.elf")
        set(EXAMPLE_INTRO "This example shows the usage of Trusted Firmware-M (TF-M). RTOS: ${RTOS_STRING}, platform: ${PLATFORM_STRING}.")
        string(CONCAT EXAMPLE_RUN_COMMAND_VHT
            "#### PSA Crypto API\n"
            "```sh\n"
            "${PLATFORM_VHT} -a ${EXAMPLE_BINARY_CRYPTO}\n"
            "```\n"
            "#### PSA Firmware Update API\n"
            "```sh\n"
            "${PLATFORM_VHT} -a ${EXAMPLE_BINARY_FIRMWARE_UPDATE}\n"
            "```\n"
        )
        if(platform STREQUAL corstone-300)
            string(CONCAT EXAMPLE_RUN_COMMAND_FVP
                "#### PSA Crypto API\n"
                "```sh\n"
                "${PLATFORM_FVP} -a ${EXAMPLE_BINARY_CRYPTO}\n"
                "```\n"
                "#### PSA Firmware Update API\n"
                "```sh\n"
                "${PLATFORM_FVP} -a ${EXAMPLE_BINARY_FIRMWARE_UPDATE}\n"
                "```\n"
            )
        elseif(platform STREQUAL corstone-310)
            string(CONCAT EXAMPLE_RUN_COMMAND_FVP
                "**Note A standalone Arm Ecosystem FVP is not available for Corstone-310, only the version included in the Keil MDK**"
                "#### PSA Crypto API\n"
                "```sh\n"
                "${PLATFORM_FVP} -a ${EXAMPLE_BINARY_CRYPTO}\n"
                "```\n"
                "#### PSA Firmware Update API\n"
                "```sh\n"
                "${PLATFORM_FVP} -a ${EXAMPLE_BINARY_FIRMWARE_UPDATE}\n"
                "```\n"
            )
        else()
            set(EXAMPLE_RUN_COMMAND_FVP "A Fixed Virtual Platform is not available for ${platform}")
        endif()
        set(example_dir ${EXAMPLES_OUTPUT_DIR}/${example}/${rtos}/${platform})

        configure_file(CMakeLists.txt.in ${example_dir}/CMakeLists.txt @ONLY)
        configure_file(${CMSIS_PACK_PLATFORM_CONFIG} ${example_dir}/cmsis-pack-platform.cmake @ONLY)
        configure_file(${README_TEMPLATE_FILE} ${example_dir}/README.md @ONLY)
        configure_file(${LINKER_SCRIPT_GCC} ${example_dir}/gcc.ld @ONLY)
        configure_file(${LINKER_SCRIPT_ARM} ${example_dir}/armclang.sct @ONLY)

        file(COPY
            main.c
            crypto.c
            firmware_update.c
            crypto.log
            firmware_update.log
            .ci_hooks
            DESTINATION ${example_dir}
        )
        file(COPY
            cmsis-pack-platform/RTE/Device/${CMSIS_PACK_PLATFORM_DEVICE_NAME}
            DESTINATION ${example_dir}/cmsis-pack-platform/RTE/Device
        )
        if(rtos STREQUAL "cmsis-threadx")
            file(COPY threadx-config DESTINATION ${example_dir})
        endif()
    endforeach()
endforeach()
